﻿YAHOO.example.init = function () {
    function onButtonClick(p_oEvent) {
        switch (this.get("id")) {
            case "btnSave":
                Save();
                break;
            case "btnClose":
                Close();
                break;
            case "btnNew1":
                handleAdd();
                break;
            case "btnDelete1":
                handleDelete();
                break;
            default:
                break;
        }
    }
    YAHOO.util.Event.onContentReady("toolbar", function () {
        btnSave = new YAHOO.widget.Button("btnSave");
        btnSave.on("click", onButtonClick);
        btnClose = new YAHOO.widget.Button("btnClose");
        btnClose.on("click", onButtonClick);
        if (flag == "1") {
            btnSave._button.style.display = "none";
            $("#btnSave").attr('disabled', "true");
        }
    });
    YAHOO.util.Event.onContentReady("con_two_1", function () {
        btnNews1 = new YAHOO.widget.Button("btnNew1");
        btnNews1.on("click", onButtonClick);
        btnDeletes1 = new YAHOO.widget.Button("btnDelete1");
        btnDeletes1.on("click", onButtonClick);
        if (flag == "1") {
            btnNews1._button.style.display = "none";
            $("#btnNews1").attr('disabled', "true");
            btnDeletes1._button.style.display = "none";
            $("#btnDeletes1").attr('disabled', "true");
        }
    });
} ();


var format1 = function (elCell, oRecord, oColumn, sData) {
    if (action == "insert" || flag == "2") {
        var str1 = [];
        var id = "id" + Math.random();
        str1.push("<option value='00'> </option>");
        if (flag == "1") {
            str1.push("<select id='" + id + "' disabled='disabled' name='Caption' style='width:150px;'>");
        } else {
            str1.push("<select id='" + id + "' onchange='ChangeEvent1(this);' name='Caption' style='width:150px;'>");
        }
        str1.push("<option value='00'> </option>");
        for (var i = 0; i < CrmProduct.length; i++) {
            if (CrmProduct[i].Caption == sData) {
                str1.push("<option value='" + CrmProduct[i].Caption + "' selected>" + CrmProduct[i].Caption + "</option>");
            } else {
                str1.push("<option value='" + CrmProduct[i].Caption + "'>" + CrmProduct[i].Caption + "</option>");
            }
        }
        str1.push("</select>");
        elCell.innerHTML = str1.join("");
    } else {
        if (flag == "1") {
            if (sData == null || sData == "") {
                elCell.innerHTML = "<input type=\"text\" value=\"\" disabled='disabled' name='Caption' style='width:150px; background: #e7eaf4;'>";
            } else {
                elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Caption' style='width:150px; background: #e7eaf4;'>";
            }
        }
    }
}
var format2 = function (elCell, oRecord, oColumn, sData) {
    elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Code' style='width:95%; background: #e7eaf4;'>";
}
var format3 = function (elCell, oRecord, oColumn, sData) {
    if (sData == null || sData == "") {
        elCell.innerHTML = "<input type=\"text\" value=\"\" disabled='disabled' name='PhraseText' style='width:95%; background: #e7eaf4;'>";
    } else {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='PhraseText' style='width:95%; background: #e7eaf4;'>";
    }
}
var format4 = function (elCell, oRecord, oColumn, sData) {
    elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Money' style='width:95%; background: #e7eaf4;'>";
}
var format5 = function (elCell, oRecord, oColumn, sData) {
    if (flag == "1") {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Rebate' style='width:95%;'>";
    } else {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" readonly='readonly' name='Rebate' onchange='ChangeEvent1(this);' style='width:95%;'>";
    }
}
var format6 = function (elCell, oRecord, oColumn, sData) {
    if (flag == "1") {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Amount' style='width:95%;'>";
    } else {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" readonly='readonly' name='Amount' onchange='ChangeEvent1(this);' style='width:95%;'>";
    }
}
var format7 = function (elCell, oRecord, oColumn, sData) {
    if (action == "insert" || flag == "2") {
        var str1 = [];
        var id = "id" + Math.random();
        str1.push("<option value='00'> </option>");
        if (flag == "1") {
            str1.push("<select id='" + id + "' disabled='disabled' name='UnitId' style='width:95%;'>");
        } else {
            str1.push("<select id='" + id + "' onchange='ChangeEvent1(this);' name='UnitId' style='width:95%;'>");
        }
        str1.push("<option value='00'> </option>");
        for (var i = 0; i < CrmListUnitId.length; i++) {
            if (CrmListUnitId[i].PhraseID == sData) {
                str1.push("<option value='" + CrmListUnitId[i].PhraseID + "' selected>" + CrmListUnitId[i].PhraseText + "</option>");
            } else {
                str1.push("<option value='" + CrmListUnitId[i].PhraseID + "'>" + CrmListUnitId[i].PhraseText + "</option>");
            }
        }
        str1.push("</select>");
        elCell.innerHTML = str1.join("");
    }
}
var format8 = function (elCell, oRecord, oColumn, sData) {
    elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='RebateMoney' style='width:95%; background: #e7eaf4;'>";
}
var format9 = function (elCell, oRecord, oColumn, sData) {
    if (flag == "1") {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='AccountReceivable' style='width:95%;'>";
    } else {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" readonly='readonly' name='AccountReceivable' onchange='ChangeEvent1(this);' style='width:95%;'>";
    }
}
var format10 = function (elCell, oRecord, oColumn, sData) {
    elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='TypeId' style='width:95%; background: #e7eaf4;'>";
}
var format11 = function (elCell, oRecord, oColumn, sData) {
    if (sData == null || sData == "") {
        elCell.innerHTML = "<input type=\"text\" value=\"\" disabled='disabled' name='UnitType' style='width:95%; background: #e7eaf4;'>";
    } else {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='UnitType' style='width:95%; background: #e7eaf4;'>";
    }
}
var format12 = function (elCell, oRecord, oColumn, sData) {
    elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='ProductId' style='width:95%; background: #e7eaf4;'>";
}
YAHOO.util.Event.addListener(window, "load", function () {
    BindData();
    hideCol();
});
var BindData = function () {
    YAHOO.example.InlineCellEditing = function () {
        var myColumnDefs1 = [
            { key: "ID", label: "ID", resizeable: true },
            { key: "ProductId", label: "ProductId", resizeable: true, formatter: format12 },
            { key: "TypeId", label: "产品类型", resizeable: true, formatter: format10 },
            { key: "Caption", label: "产品名称", resizeable: true, formatter: format1 },
            { key: "Code", label: "产品编码", resizeable: true, formatter: format2 },
            { key: "PhraseText", label: "产品类型", resizeable: true, formatter: format3 },
            { key: "Money", label: "定价", resizeable: true, formatter: format4,width:80 },
            { key: "UnitId", label: "计量单位", resizeable: true, formatter: format7, },
            { key: "UnitType", label: "计量单位", resizeable: true, formatter: format11 },
            { key: "Amount", label: "数量", resizeable: true, formatter: format6,
                editor: new YAHOO.widget.TextboxCellEditor({validator:YAHOO.widget.DataTable.validateNumber})},
            { key: "Rebate", label: "折扣率(%)", resizeable: true, formatter: format5,
                editor: new YAHOO.widget.TextboxCellEditor({validator:YAHOO.widget.DataTable.validateNumber})},
            { key: "RebateMoney", label: "折扣金额", resizeable: true, formatter: format8 },
            { key: "AccountReceivable", label: "应收金额", resizeable: true, formatter: format9,
                editor: new YAHOO.widget.TextboxCellEditor({validator:YAHOO.widget.DataTable.validateNumber})}
            ];
        myDataSource1 = new YAHOO.util.DataSource([{ ID: "", ProductId: "", Code: "", TypeId: "", UnitType: "", Caption: "",
            Money: "", Rebate: "", Amount: "", UnitId: "", UnitType: "", RebateMoney: "",
            ProductResignMoney: "", BeResignMoney: "", AccountReceivable: "", StateId: ""
        }]);
        myDataSource1.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
        myDataSource1.responseSchema = {
            fields: ["ID", "ProductId", "Code", "TypeId", "PhraseText", "Caption", "Money", "Rebate", "Amount",
                     "UnitId", "UnitType", "RebateMoney", "ProductResignMoney", "BeResignMoney", "AccountReceivable", "StateId"]
        };
        var myDataTable1 = new YAHOO.widget.DataTable("con_1",
            myColumnDefs1, myDataSource1, { selectionMode: "single" });
        mDataTable1 = myDataTable1;
        var i1 = 1,
            bReverseSorted1 = false;
        var trackReverseSorts1 = function (oArg) {
            bReverseSorted1 = (oArg.dir === YAHOO.widget.DataTable.CLASS_DESC);
        };
        var onEventSaveCellEditor = function (oArgs) {
            setValue1(oArgs.editor._oColumn.key, oArgs.newData);
        }
        myDataTable1.subscribe("rowMouseoverEvent", myDataTable1.onEventHighlightRow);
        myDataTable1.subscribe("rowMouseoutEvent", myDataTable1.onEventUnhighlightRow);
        myDataTable1.subscribe("rowClickEvent", myDataTable1.onEventSelectRow); //mousedown
        myDataTable1.subscribe("cellClickEvent", myDataTable1.onEventShowCellEditor);
        myDataTable1.subscribe("editorSaveEvent", onEventSaveCellEditor);
        myDataTable1.subscribe("checkboxClickEvent", function (oArgs) {
            var elCheckbox = oArgs.target;
            var oRecord = this.getRecord(elCheckbox);
            oRecord.setData("iSun_Selected", elCheckbox.checked);
        });
        GetFormValue();
        return {
            ds: myDataSource1,
            dt: myDataTable1
        };
    } ();
}
//隐藏/栏位
function hideCol() {
    mDataTable1.hideColumn(parseInt(0)); mDataTable1.refreshView();
    mDataTable1.hideColumn(parseInt(1)); mDataTable1.refreshView();
    mDataTable1.hideColumn(parseInt(2)); mDataTable1.refreshView();
    if (flag == "1") {
        mDataTable1.hideColumn(parseInt(7)); mDataTable1.refreshView();
    } else {
        mDataTable1.hideColumn(parseInt(8)); mDataTable1.refreshView();
    }
}

//设置值1
var GetFormValue = function () {
    for (var i = 0; i < PackDetail.length; i++) {
        SetFormValue(PackDetail);
    }
}
var SetFormValue = function (obj) {
    var arr = eval(obj);
    myDataSource1.liveData = arr;
    mDataTable1.getDataSource().sendRequest(null, { success: mDataTable1.onDataReturnInitializeTable }, mDataTable1);
}

//值改变事件
var ChangeEvent1 = function (oArgs) {
    var f = oArgs.name;
    var v = oArgs.value;
    setValue1(f, v);
}
//f=字段名 v=值
var record;
var setValue1 = function (f, v) {
    var recordID = mDataTable1.getSelectedRows()[0];
    if (recordID != null && recordID.length != 1) {
        record = mDataTable1.getRecord(recordID);
        record._oData[f] = v;
        for (var i = 0; i < CrmProduct.length; i++) {
            if (CrmProduct[i].Caption == v) {
                record._oData["TypeId"] = CrmProduct[i].TypeId;
                record._oData["ProductId"] = CrmProduct[i].Productid;
                record._oData["PhraseText"] = CrmProduct[i].PhraseText;
                record._oData["Code"] = CrmProduct[i].Code;
                record._oData["Money"] = CrmProduct[i].Money;
                $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=TypeId]")[0]).val(CrmProduct[i].TypeId);
                $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=ProductId]")[0]).val(CrmProduct[i].Productid);
                $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=PhraseText]")[0]).val(CrmProduct[i].PhraseText);
                $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=Code]")[0]).val(CrmProduct[i].Code);
                $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=Money]")[0]).val(CrmProduct[i].Money);
            }
        }
        if (f == "Caption") {
            setTextValue();
        }
        if (f == "Amount") {
            if (isNaN(v) || v == "0") {
                alert("数量必须输入数字,且不能为0!");
                $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=Amount]")[0]).val("");
                record._oData["Amount"] = "";
            }
            $("#TotalRebate").val("");
            setTextValue();
        }
        if (f == "AccountReceivable") {
            if (isNaN(v)) {
                alert("应收金额必须输入数字!");
                $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=AccountReceivable]")[0]).val("");
                record._oData["AccountReceivable"] = "";
            }
            $("#TotalRebate").val("");
            setTextValueReceivable();
        }
        if (f == "Rebate") {
            if (isNaN(v)) {
                alert("折扣率必须输入数字!");
                $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=Rebate]")[0]).val("");
                record._oData["Rebate"] = "";
            } else {
                if (v > 100) {
                    /*alert("折扣率不能超过100%!");
                    $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=Rebate]")[0]).val("");
                    record._oData["Rebate"] = "";*/
                }
            }
            $("#TotalRebate").val("");
            setTextValue();
        }
    } else {
        alert("请先选中当前行!");
    }
}
var setTextValue = function () {
    var vM = record._oData["Money"];
    if (vM == "") {
        alert("请选择产品!");
        return;
    } else {
        var vMoney = record._oData["Money"];
        var vAmount = 1;
        var vRebate = 100;
        if (record._oData["Amount"] != "") {
            vAmount = record._oData["Amount"];
        }
        if (record._oData["Rebate"] != "") {
            vRebate = record._oData["Rebate"];
        }
        var vRebateMoney = (vMoney * vAmount) - (vMoney * vAmount * (vRebate / 100));
        var vAccountReceivable = (vMoney * vAmount) - vRebateMoney;
        $($($(".yui-dt-selected")[0]).find("input[name=RebateMoney]")[0]).val(vRebateMoney.toFixed(2));
        //$($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=RebateMoney]")[0]).val(vRebateMoney.toFixed(2));
        record._oData["RebateMoney"] = "" + vRebateMoney.toFixed(2) + "";
        //$($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=AccountReceivable]")[0]).val(vAccountReceivable.toFixed(2));
        $($($(".yui-dt-selected")[0]).find("input[name=AccountReceivable]")[0]).val(vAccountReceivable.toFixed(2));
        record._oData["AccountReceivable"] = "" + vAccountReceivable.toFixed(2) + "";

        var list = mDataTable1.getRecordSet()._records;
        var cValue = 0;
        for (var i = 0; i < list.length; i++) {
            if (list[i]._oData.AccountReceivable != "") {
                var v = Number(list[i]._oData.AccountReceivable);
                cValue += v;
            }
        }
        $('#TotalMoney').val(cValue.toFixed(2));
    }
}
var setTextValueReceivable = function () {
    var vM = record._oData["Money"];
    if (vM == "") {
        alert("请选择产品!");
        return;
    } else {
        var vMoney = record._oData["Money"];
        var vAmount = 1;
        var vRebate = 100;
        if (record._oData["Amount"] != "") {
            vAmount = record._oData["Amount"];
        } else {
            alert("请输入数量!");
            return;
        }
        if (record._oData["Rebate"] != "") {
            vRebate = record._oData["Rebate"];
        }
        if (record._oData["AccountReceivable"] == "") {
            $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=AccountReceivable]")[0]).val("");
            record._oData["AccountReceivable"] = "";
        } else {
            var AccountReceivable = record._oData["AccountReceivable"];
            vRebate = (AccountReceivable / (vMoney * vAmount)) * 100;
            $($($(".yui-dt-selected")[0]).find("input[name=Rebate]")[0]).val(vRebate.toFixed(2));
            //$($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=Rebate]")[0]).val(vRebate.toFixed(2));
            record._oData["Rebate"] = "" + vRebate.toFixed(2) + "";
            var vRebateMoney = (vMoney * vAmount) - (vMoney * vAmount * (vRebate / 100));
            $($($(".yui-dt-selected")[0]).find("input[name=RebateMoney]")[0]).val(vRebateMoney.toFixed(2));
            //$($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=RebateMoney]")[0]).val(vRebateMoney.toFixed(2));
            record._oData["RebateMoney"] = "" + vRebateMoney.toFixed(2) + "";
        }
        var list = mDataTable1.getRecordSet()._records;
        var cValue = 0;
        for (var i = 0; i < list.length; i++) {
            if (list[i]._oData.AccountReceivable != "") {
                var v = Number(list[i]._oData.AccountReceivable);
                cValue += v;
            }
        }
        $('#TotalMoney').val(cValue.toFixed(2));
    }
}

//新增行
var handleAdd = function () {
    mDataTable1.set("sortedBy", null);
    index = mDataTable1.getRecordSet()._records.length;
    var entity = [{ ID: "", Code: "", TypeId: "", Caption: "", Money: "", Rebate: ""
                    , Amount: "", UnitId: "", RebateMoney: "", AccountReceivable: ""
                    , StateId: ""
    }];
    mDataTable1.addRows(entity, index);
}
//产品删除行
var handleDelete = function () {
    var recordID = mDataTable1.getSelectedRows()[0];
    if (recordID != null && recordID.length != 1) {
        if (mDataTable1.getRecordSet()._records.length == 1) {
            alert("最后一行不能删除!");
            return;
        } else {
            var PackId = $('#ID').val();
            record = mDataTable1.getRecord(recordID);
            var vid = record.getData("ID");
            if (vid == "" || vid == "00000000-0000-0000-0000-000000000000") {
                mDataTable1.deleteRow(recordID);
                LoadAccountReceivable();
            } else {
                if (confirm("您确定删除该行吗?")) {
                    var SaveUrl = Url + "?r=" + Math.random() + "&action=delete&entity=CrmPackDetail&json=&pid=" + PackId + "&id=" + vid;
                    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                        success: function (res) {
                            try {
                                var str = res.responseText;
                                if (str == "1") {
                                    alert("删除成功!");
                                    mDataTable1.deleteRow(recordID);
                                    LoadAccountReceivable();
                                }
                                else {
                                    alert(str + "\n" + "数据删除失败!");
                                    return;
                                }
                            }
                            catch (e) {
                            }
                            finally {
                            }
                        },
                        failure: function (error) {
                            alert("数据错误：" + error.statusText);
                        }
                    }, null);
                }
            }
        }
    } else {
        alert("请选中一行后删除！");
    }
}
function LoadAccountReceivable() {
    var list = mDataTable1.getRecordSet()._records;
    var cValue = 0;
    for (var i = 0; i < list.length; i++) {
        if (list[i]._oData.AccountReceivable != "") {
            var v = parseInt(list[i]._oData.AccountReceivable);
            cValue += v;
        }
    }
    $('#TotalMoney').val(cValue);
}
//刷新父窗体数据
function RefreshDataParent() {
    if (parent.NeedRefreshPage != null) {
        if (typeof (parent.NeedRefreshPage.LoadData) == "function") {
            parent.NeedRefreshPage.RefreshData();
        }
    }
}
//总折扣率
function TotalRebateChange(value) {
    if (value != "") {
        var list = mDataTable1.getRecordSet()._records;
        var elist = [];
        var TotalRebate = $("#TotalRebate").val();
        for (var i = 0; i < list.length; i++) {
            if (list[i]._oData.ID == "" || list[i]._oData.ID == null) {
                list[i]._oData.ID = "00000000-0000-0000-0000-000000000000";
            }
            if (list[i]._oData.Code == "" || list[i]._oData.Code == "00" || list[i]._oData.Code == null) {
                alert("第" + (i + 1) + "行 产品名称不能为空!");
                return;
            }
            if (list[i]._oData.UnitId == "" || list[i]._oData.UnitId == "00" || list[i]._oData.UnitId == null) {
                alert("第" + (i + 1) + "行 计量单位不能为空!");
                return;
            }
            if (list[i]._oData.Amount == "" || list[i]._oData.Amount == null) {
                alert("第" + (i + 1) + "行 数量不能为空且不能为0!");
                return;
            }
            if (list[i]._oData.Rebate == "" || list[i]._oData.Rebate == null) {
                alert("第" + (i + 1) + "行 折扣率不能为空!");
                return;
            }
            if (list[i]._oData.RebateMoney == "" || list[i]._oData.RebateMoney == null) {
                alert("第" + (i + 1) + "行 折扣金额不能为空!");
                return;
            }
            if (list[i]._oData.AccountReceivable == "" || list[i]._oData.AccountReceivable == null) {
                alert("第" + (i + 1) + "行 应该收金额不能为空!");
                return;
            }
            if (TotalRebate != "") {  //总折扣率
                var Rebate = list[i]._oData.Rebate;
                var Money = list[i]._oData.Money;
                var Amount = list[i]._oData.Amount;
                var vAccount = Money * Amount * ((Rebate * TotalRebate / 100) / 100);
                var vRebate = (Money * Amount) - vAccount;
                //数量
                list[i]._oData.Amount = Amount;
                //折扣率
                var v = (Rebate * TotalRebate / 100);
                list[i]._oData.Rebate = v.toFixed(2);
                //折扣金额
                list[i]._oData.RebateMoney = vRebate.toFixed(2);
                //应收金额
                list[i]._oData.AccountReceivable = vAccount.toFixed(2);
            }
            elist.push(list[i]._oData);
            elist[i].OrderID = $('#ID').val();          
        }
        if (confirm("您确定总折扣率为 " + value + "%")) {
            SetFormValue(elist);
            var vTotalMoney = $('#TotalMoney').val();
            var vTotalRebateMoney = vTotalMoney * (value / 100);
            $('#TotalMoney').val(vTotalRebateMoney.toFixed(2));
            return;
        } else {
            $('#TotalRebate').val("");
        }
    }
}

//取值
function setValue() {
    instance[0].ID = $("#ID").val();
    instance[0].Packname = $("#Packname").val();
    instance[0].Remark = $("#Remark").val();
    instance[0].AllMoney = $("#TotalMoney").val();
    instance[0].AllRebate = $("#TotalRebate").val();
}

//保存
var Save = function () {
    setValue();
    if (instance[0].Packname == "") {
        alert("产品包名称不能为空!");
        $("#Packname").focus();
        return;
    }
    var list = mDataTable1.getRecordSet()._records;
    var elist = [];
    instance[0].ProduectList = "";
    var TotalRebate = $("#TotalRebate").val();
    for (var i = 0; i < list.length; i++) {
        if (list[i]._oData.ID == "" || list[i]._oData.ID == null) {
            list[i]._oData.ID = "00000000-0000-0000-0000-000000000000";
        }
        if (list[i]._oData.Code == "" || list[i]._oData.Code == "00" || list[i]._oData.Code == null) {
            alert("第" + (i + 1) + "行 产品名称不能为空!");
            return;
        }
        if (list[i]._oData.UnitId == "" || list[i]._oData.UnitId == "00" || list[i]._oData.UnitId == null) {
            alert("第" + (i + 1) + "行 计量单位不能为空!");
            return;
        }
        if (list[i]._oData.Amount == "" || list[i]._oData.Amount == null) {
            alert("第" + (i + 1) + "行 数量不能为空且不能为0!");
            return;
        }
        if (list[i]._oData.Rebate == "" || list[i]._oData.Rebate == null) {
                alert("第" + (i + 1) + "行 折扣率不能为空!");
                return;
            }
            if (list[i]._oData.RebateMoney == "" || list[i]._oData.RebateMoney == null) {
                alert("第" + (i + 1) + "行 折扣金额不能为空!");
                return;
            }
            if (list[i]._oData.AccountReceivable == "" || list[i]._oData.AccountReceivable == null) {
                alert("第" + (i + 1) + "行 应该收金额不能为空!");
                return;
            }
        if (TotalRebate != "") {  //总折扣率
            /*var Rebate = list[i]._oData.Rebate;
            var Money = list[i]._oData.Money;
            var Amount = list[i]._oData.Amount;
            var vAccount = Money * Amount * ((Rebate * TotalRebate / 100) / 100);
            var vRebate = (Money * Amount) - vAccount;

            //数量
            list[i]._oData.Amount = Amount;
            //折扣率
            var v = (Rebate * TotalRebate / 100);
            list[i]._oData.Rebate = v.toFixed(2);
            //折扣金额
            list[i]._oData.RebateMoney = vRebate.toFixed(2);
            //应收金额
            list[i]._oData.AccountReceivable = vAccount.toFixed(2);*/
        }
        elist.push(list[i]._oData);
        elist[i].OrderID = $('#ID').val();
    }
    for (var j = 0; j < elist.length; j++) {
        instance[0].ProduectList += "@" + elist[j].ID + "︾" + elist[j].ProductId + "︾" +
                                            elist[j].Rebate + "︾" + elist[j].Amount + "︾" + elist[j].UnitId + "︾" +
                                            elist[j].RebateMoney + "︾" + elist[j].AccountReceivable + "︾";
    }
    if (instance[0].AllMoney == "" || instance[0].AllMoney == null) {
        alert("总金额不能为空!");
        $('#TotalMoney').focus();
        return;
    }
    if (confirm("您确定保存当前信息吗?")) {
        var json = $.toJSON(instance[0]).replace(/'/g, "[B]");
        $("#jsonDataContent").val(json);
        trySave();
    }
}
var trySave = function () {
    var formObject = document.getElementById('form2');
    YAHOO.util.Connect.setForm(formObject);
    var cObj = YAHOO.util.Connect.asyncRequest('POST', Url + "?flag=1&action=insert&type=" + flag + "&entity=CrmProductPack", callback);
}
var responseSuccess = function (res) {
    try {
        var str = res.responseText;
        if (str == "1") {
            alert("数据保存成功");
            RefreshDataParent(); //刷新父页面
            Close(); //关闭当前页
        } else {
            alert(str);
        }
    } catch (e) {
    }
    finally {
    }
};
var responseFailure = function (error) {
    alert("数据错误：" + error.statusText);
}
var callback = {
    success: responseSuccess,
    failure: responseFailure
};